#include "stdafx.h"
#include "CollectForumal.h"
#include "InstanceTouch.h"
#include <algorithm>
DECL_INSTANCE_TOUCH(ETouch_CollectForumal);

struct  CollectForumalDefReflectionRegister
{
	CollectForumalDefReflectionRegister()
	{
		REGISTER_REFLECTION_MEMBER(CollectFormulaDef,m_iID,INVALID_ID).Editor_SetAsPrimaryKey();
		REGISTER_REFLECTION_MEMBER(CollectFormulaDef,m_sDisplayName,std::string("DefaultName")).Editor_SetAsDisplayName();
		REGISTER_REFLECTION_MEMBER(CollectFormulaDef,m_iOrder,0).Editor_SetAsRaw();
		
		REGISTER_REFLECTION_MEMBER(CollectFormulaDef,m_iResultCardType,INVALID_ID).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_CardTypes,"Result");
		REGISTER_REFLECTION_MEMBER(CollectFormulaDef,m_iResultHeroType,INVALID_ID).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_HeroTypes,"Result");

		REGISTER_REFLECTION_MEMBER(CollectFormulaDef,m_iCardType0,INVALID_ID).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_CardTypes,"CardType0");
		REGISTER_REFLECTION_MEMBER(CollectFormulaDef,m_iHeroType0,INVALID_ID).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_HeroTypes,"CardType0");

		REGISTER_REFLECTION_MEMBER(CollectFormulaDef,m_iCardType1,INVALID_ID).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_CardTypes,"CardType1");
		REGISTER_REFLECTION_MEMBER(CollectFormulaDef,m_iHeroType1,INVALID_ID).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_HeroTypes,"CardType1");

		REGISTER_REFLECTION_MEMBER(CollectFormulaDef,m_iCardType2,INVALID_ID).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_CardTypes,"CardType2");
		REGISTER_REFLECTION_MEMBER(CollectFormulaDef,m_iHeroType2,INVALID_ID).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_HeroTypes,"CardType2");

		REGISTER_REFLECTION_MEMBER(CollectFormulaDef,m_iCardType3,INVALID_ID).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_CardTypes,"CardType3");
		REGISTER_REFLECTION_MEMBER(CollectFormulaDef,m_iHeroType3,INVALID_ID).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_HeroTypes,"CardType3");

		REGISTER_REFLECTION_MEMBER(CollectFormulaDef,m_iCardType4,INVALID_ID).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_CardTypes,"CardType4");
		REGISTER_REFLECTION_MEMBER(CollectFormulaDef,m_iHeroType4,INVALID_ID).Editor_SetAsForeignKey(eDefGroup_Default,eDefaultDef_HeroTypes,"CardType4");


		GlobalClassManager::Instance().RegisterClass(CollectFormulaDef::s_GetReflection(),eDefaultDef_CollectForumal);
		GlobalDefManager::Instance().RegisterDefManger(&CollectForumalManager::Instance());

	}
}___CollectForumalDefReflectionRegister;

rewrite  void CollectFormulaDef::OnArchieveReaded()
{
	m_Forumal.Reset();
	m_Forumal.m_iOrder=m_iOrder;
	m_Forumal.m_iID=m_iID;

	if(m_iResultCardType!=INVALID_ID) { m_Forumal.m_cResult.m_iObjType=1;m_Forumal.m_cResult.m_iTypeID=m_iResultCardType;}
	if(m_iResultHeroType!=INVALID_ID) { m_Forumal.m_cResult.m_iObjType=2;m_Forumal.m_cResult.m_iTypeID=m_iResultHeroType;}

	if(m_iCardType0!=INVALID_ID) { m_Forumal.m_vRecipe[0].m_iObjType=1;m_Forumal.m_vRecipe[0].m_iTypeID=m_iCardType0;}
	if(m_iHeroType0!=INVALID_ID) { m_Forumal.m_vRecipe[0].m_iObjType=2;m_Forumal.m_vRecipe[0].m_iTypeID=m_iHeroType0;}

	if(m_iCardType1!=INVALID_ID) { m_Forumal.m_vRecipe[1].m_iObjType=1;m_Forumal.m_vRecipe[1].m_iTypeID=m_iCardType1;}
	if(m_iHeroType1!=INVALID_ID) { m_Forumal.m_vRecipe[1].m_iObjType=2;m_Forumal.m_vRecipe[1].m_iTypeID=m_iHeroType1;}

	if(m_iCardType2!=INVALID_ID) { m_Forumal.m_vRecipe[2].m_iObjType=1;m_Forumal.m_vRecipe[2].m_iTypeID=m_iCardType2;}
	if(m_iHeroType2!=INVALID_ID) { m_Forumal.m_vRecipe[2].m_iObjType=2;m_Forumal.m_vRecipe[2].m_iTypeID=m_iHeroType2;}

	if(m_iCardType3!=INVALID_ID) { m_Forumal.m_vRecipe[3].m_iObjType=1;m_Forumal.m_vRecipe[3].m_iTypeID=m_iCardType3;}
	if(m_iHeroType3!=INVALID_ID) { m_Forumal.m_vRecipe[3].m_iObjType=2;m_Forumal.m_vRecipe[3].m_iTypeID=m_iHeroType3;}

	if(m_iCardType4!=INVALID_ID) { m_Forumal.m_vRecipe[4].m_iObjType=1;m_Forumal.m_vRecipe[4].m_iTypeID=m_iCardType4;}
	if(m_iHeroType4!=INVALID_ID) { m_Forumal.m_vRecipe[4].m_iObjType=2;m_Forumal.m_vRecipe[4].m_iTypeID=m_iHeroType4;}

	m_pNextFormula=NULL;
}
static bool __CompareByOrder(const CollectFormulaDef* pA,const CollectFormulaDef* pB)
{
	return pA->m_iOrder<pB->m_iOrder;
}
void CollectForumalManager::OnArchieveReaded()
{
	__CollectForumalManager::OnArchieveReaded();

	std::vector<CollectFormulaDef*> vItems;
	EnumerateEditable(vItems);




	std::sort(vItems.begin(),vItems.end(),__CompareByOrder);
	
	if(vItems.size()>0)
	{ 
		m_pFirstFormula=vItems[0];
	}
	else
	{
		m_pFirstFormula= NULL;
	}

	for(uint32 ii=0;ii<vItems.size();ii++)
	{
		if(ii==vItems.size()-1)
		{
			vItems[ii]->m_pNextFormula=m_pFirstFormula;
		}
		else
		{
			vItems[ii]->m_pNextFormula=vItems[ii+1];
		}
		
	}


}